﻿using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;

namespace NET8.Domain.ValidAttribute
{
    /// <summary>
    /// MobileAttribute
    /// </summary>
    public class MobileAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value is string phoneNumber && !Regex.IsMatch(phoneNumber, @"^1[3-9]\d{9}$"))
            {
                return new ValidationResult(ErrorMessage ?? "请输入正确的手机号码");
            }

            return ValidationResult.Success;
        }
    }
}